﻿
@{
    ViewBag.Title = "EquipPriceEditView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>设备信息</legend>
</fieldset>
<form class="layui-form" id="FormInfo">
    <div class="layui-form-item">
        <label class="layui-form-label">材料名称</label>
        <div class="layui-input-inline" style="width:150px;">
            <input type="text" id="EquipName" name="EquipName" value="" required lay-verify="required" placeholder="请输入材料名称" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label" style="width:60px;">规格型号</label>
        <div class="layui-input-inline" style="width:200px;">
            <input type="text" id="Specification" name="Specification" required value="" lay-verify="required" placeholder="请输入规格型号" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label" style="width:30px;">类型</label>
        <div class="layui-input-inline" style="width:120px;">
            @*<input type="text" id="Type" name="Type" value="" placeholder="" autocomplete="off" class="layui-input">*@
            <select name="Type" id="Type">
                <option value="电缆">电缆</option>
                <option value="电缆附件">电缆附件</option>
                <option value="设备">设备</option>
                <option value="架空">架空</option>
            </select>
        </div>
        <label class="layui-form-label" style="width:30px;">单位</label>
        <div class="layui-input-inline" style="width:50px;">
            <input type="text" id="Unit" name="Unit" value="" placeholder="" autocomplete="off" class="layui-input">
        </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">一般名称</label>
        <div class="layui-input-inline" style="width:150px;">
            <input type="text" id="NormalName" name="NormalName" value="" required lay-verify="required" placeholder="请输入一般名称" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label" style="width:60px;">统一型号</label>
        <div class="layui-input-inline" style="width:200px;">
            <input type="text" id="NormalSpeci" name="NormalSpeci" required value="" lay-verify="required" placeholder="请输入统一型号" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label" style="width:60px;">参考单价</label>
        <div class="layui-input-inline" style="width:100px;">
            <input type="number" id="NormalPrice" name="NormalPrice" required value="" lay-verify="required" placeholder="参考单价" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label" style="width:45px;">分类一</label>
        <div class="layui-input-inline" style="width:120px;">
            @*<input type="text" id="Type" name="Type" value="" placeholder="" autocomplete="off" class="layui-input">*@
            <select name="TypeL1" id="TypeL1">
                <option value="高压电缆">高压电缆</option>
                <option value="低压电缆">低压电缆</option>
                <option value="二次电缆">二次电缆</option>
                <option value="电缆附件">电缆附件</option>
                <option value="变压器">变压器</option>
                <option value="高压柜">高压柜</option>
                <option value="低压柜">低压柜</option>
                <option value="柴发屏">柴发屏</option>
                <option value="封闭母线">封闭母线</option>
                <option value="其他设备">其他设备</option>
                <option value="附件">附件</option>
                <option value="架空">架空材料</option>
            </select>
        </div>
        <div class="layui-input-inline" style="width:100px;">
            <button type="submit" id="PriceForm" class="layui-btn" lay-submit lay-filter="PriceFormVerify" style="display:none;">立即提交</button>
            <button type="button" class="layui-btn" style="float:left;" id="BtnSave"><i class="layui-icon">&#xe621;</i>保存</button>
        </div>
    </div>

</form>

<script type="text/template" id="PreLayerForm">
    @*一下代码为进入该页面时，弹出框设置*@
    <form class="layui-form" id="PreAdd">
        <div class="layui-form-item" style="margin-top:10px;">
            <label class="layui-form-label">材料名称</label>
            <div class="layui-input-inline" style="width:150px;">
                <input type="text" name="EquipName" value="" required lay-verify="required" placeholder="请输入材料名称" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-inline" style="width:200px;">
                <input type="text" name="Specification" required value="" lay-verify="required" placeholder="请输入规格型号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline" style="width:200px;">
                <select name="Type">
                    <option value="电缆">电缆</option>
                    <option value="电缆附件">电缆附件</option>
                    <option value="设备">设备</option>
                    <option value="架空">架空</option>
                </select>
            </div>
            <label class="layui-form-label">单位</label>
            <div class="layui-input-inline" style="width:50px;">
                <input type="text" name="Unit" value="" placeholder="" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="width:100px;">
                <button type="submit" id="PreLayerSubmit" class="layui-btn" lay-submit lay-filter="PreLayerFormVerify" style="display:none;">立即提交</button>
                <button type="button" class="layui-btn" style="float:left;" id="PreLayerBtnSave"><i class="layui-icon">&#xe621;</i>保存</button>
                @*<button type="button" class="layui-btn" style="float:left;" id="Debug"><i class="layui-icon">&#xe621;</i>Debug</button>*@
            </div>
        </div>
    </form>
</script>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>采购信息</legend>
</fieldset>
<form class="layui-form" id="FormSingleInfo">
    <div class="layui-form-item">
        <label class="layui-form-label">供应商名称</label>
        <div class="layui-input-inline" style="width:200px;">
            <input type="text"  name="SellerName" value="" required lay-verify="required" placeholder="请输入材料名称" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label" style="width:60px;">采购时间</label>
        <div class="layui-input-inline" style="width:200px;">
            <input type="text" id="BuyTime" name="BuyTime" required value="" lay-verify="required" placeholder="请输入规格型号" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label" style="width:30px;">价格</label>
        <div class="layui-input-inline" style="width:120px;">
            <input type="number"  name="Price" value="" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">项目名称</label>
        <div class="layui-input-inline" style="width:450px;">
            <input type="text"  name="ProjectName" value="" placeholder="" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline" style="width:100px;">
            <button type="submit" id="PriceFormSingle" class="layui-btn" lay-submit lay-filter="SingleFormVerify" style="display:none;">立即提交</button>
            <button type="button" class="layui-btn  layui-btn-normal" style="float:left;" id="BtnSingleSave"><i class="layui-icon">&#xe621;</i>新增</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="TableList"></table>

<script type="text/javascript">
    layui.use(['form', 'laydate', 'table', 'layer'], function () {
        var form = layui.form;
        var laydate = layui.laydate;
        var table = layui.table;
        var layer = layui.layer;
        var IsValidate = 0;
        var EquipPriceId = "";
        var rowid = "";

        var LayerIndex = layer.open({
            type: 1,
            title: '新增安装物料',
            area: ['700px', '300px'],
            content: $("#PreLayerForm").html(),
            success: function (layero, index) {
                //console.log(layero, index);
                form.render();
            },
            cancel: function (index, layero) {
                if (confirm('确定要关闭么')) { //只有当点击confirm框的确定时，该层才会关闭
                    layer.close(index);
                    parent.layui.admin.events.closeThisTabs();
                }
                return false;
            }
        });

        form.on('submit(PreLayerFormVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        form.on('submit(PriceFormVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        form.on('submit(SingleFormVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        $("#Debug").on("click", function () {
            layer.close(LayerIndex);
            EquipPriceId = 10;
            $("#EquipName").val("Debug");
            $("#Specification").val("Debug");
            $("#Type").val("Debug");
            $("#Unit").val("Debug");
            table.reload('demo', {
                where: { SortName: "Id", Order: "Desc", Condition: " and EquipPriceId=" + EquipPriceId }
            });
        });

        laydate.render({
            elem: '#BuyTime' //指定元素
        });

        $("#PreLayerBtnSave").on("click", function () {
            $("#PreLayerSubmit").click();
            if (IsValidate != 1) return;
            IsValidate = 0;
            var obj = $("#PreAdd").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            $.ajax({
                type: "POST",
                url: "/PreEstimate/EquipPriceFromAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('新增成功,请增加详细信息', { icon: 6, time: 1000 });
                        //parent.table.reload("demo");
                        //var index = parent.layer.getFrameIndex(window.name);
                        //parent.layer.close(index);
                        EquipPriceId = msg.EquipPriceId;
                        $("#EquipName").val(msg.EquipName);
                        $("#Specification").val(msg.Specification);

                        $("#Type").find("option[value='" + msg.Type + "']").attr("selected", true);
                        form.render();

                        $("#Unit").val(msg.Unit);
                        layer.close(LayerIndex);
                        //table.reload('demo', {
                        //    where: { SortName: "XMMC", Order: "Desc", Condition: " and CivilPriceId=" + CivilPriceId }
                        //});

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });


        $("#BtnSave").on("click", function () {
            $("#PriceForm").click();
            if (IsValidate != 1) return;
            IsValidate = 0;
            var obj = $("#FormInfo").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            data["EquipPriceId"] = EquipPriceId;
            $.ajax({
                type: "POST",
                url: "/PreEstimate/EquipPriceFromUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功,请增加详细信息', { icon: 6, time: 1000 });
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });


        $("#BtnSingleSave").on("click", function () {
            $("#PriceFormSingle").click();
            if (IsValidate != 1) return;
            IsValidate = 0;
            var obj = $("#FormSingleInfo").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            data["EquipPriceId"] = EquipPriceId;
            $.ajax({
                type: "POST",
                url: "/PreEstimate/EquipPriceFromSingleAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('添加成功', { icon: 6, time: 1000 });
                        table.reload('demo', {
                            where: { SortName: "Id", Order: "Desc", Condition: " and EquipPriceId="+EquipPriceId }
                        });


                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/PreEstimate/EquipSingleTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "Id", Order: "Desc", Condition: " and EquipPriceId=0" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'SellerName', title: '供应商名', width: 250 }
                , { field: 'Buytime', title: '采购时间', width: 120 }
                , {
                    field: 'Price', title: '价格', width: 150,
                    templet: function (d) {
                        return dealNumber(d.Price);
                    }
                }
                , { field: 'ProjectName', title: '项目名', width: 300 }
                , {
                    field: "operation", title: "操作", width: 180, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(TableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象


            if (layEvent == 'TableDel') {
                layer.confirm('确实删除：' + data.SellerName + "->价格：" + data.Price, { icon: 3, title: '提示' }, function (index) {
                    //do something
                    $.ajax({
                        type: "POST",
                        url: "/PreEstimate/EquipPriceSingleDel",
                        async: false,
                        data: { "Id": data.Id, "EquipPriceId": EquipPriceId },
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('删除成功', { icon: 6, time: 1000 });
                                table.reload('demo');
                            } else {
                                alert(msg.result);
                            }
                        }
                    });
                    layer.close(index);
                });
                //layer.msg('暂时无法删除', { icon: 0, time: 1000 });
            }

        });






    });


    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };

</script>
